
com.sxnsx.xnaja.downloader.renren = function () {
    var pub = {};
    var priv = {};
    var tag = "com.sxnsx.xnaja.downloader.renren";

    /*
     * inherit from abs_downloader
    for (var e in com.sxnsx.xnaja.downloader.abs_downloader) {
        pub[e] = com.sxnsx.xnaja.downloader.abs_downloader[e]
    }
     * */
    this.tag = tag;
    /*
     *  downloader specified methods
     * */
    this.do_proc = function (url, proc) {
        var pages = this.emnu_page_url(url);
        //alert(pages);
        for (var each in pages){
            var photos = this.emnu_photo_url(pages[each]);
            //alert(photos);
            for (var photo_url in photos) {
                if (!this.working)
                    return;
                status_info = "Pages:" + (parseInt(each) + 1) 
                    + "/" + pages.length.toString() 
                    + "; Photo:" + (parseInt(photo_url) + 1)
                    + "/" + photos.length.toString();
                xnaja_app.ui_overlay.updata_status('download', status_info);
                link = xnaja_app.utilities.xnaja_go(photos[photo_url]).match(/<img id="photo".* src="(.*?)"/)[1];
                proc(link);
            }
        }
    }


    this.emnu_page_url = function (url) {
        var ret = [];
        var vals = [];
        var flag = '<span class="number-photo">';
        var reg = RegExp(flag);
        var src = xnaja_app.utilities.xnaja_go(url);
        var offset = src.search(reg);
        if (offset == -1) {
            alert('NO')
            return null;
        }
        offset += flag.length;
        //由于go函数得到的源码非ASCII码是乱码
        //所以只有暴力搜索相册中照片的数量
        //第1-25张 对应 231,172,172, 49, 45, 50,53, 229,188,160
        //共131张  对应 229,133,177, 49,51,49, 229,188,160
        for (var i = 0; i < 100; ++i) {
            //获取照片总数
            if (src.charCodeAt(offset + i) == 229
                && src.charCodeAt(offset + i + 1) == 133)
            {
                var t = ''
                    var j = i + 3;
                while (src.charCodeAt(offset + j) <= 57) {
                    t += src[offset + j];
                    j+=1;
                }
                this.total_photo = parseInt(t);
                break;
            }
        }
        if (this.total_photo <= 16){
            this.total_page = 1;
        } else {
            this.total_page = parseInt(this.total_photo / 16);
            if (this.total_photo % 16 != 0)
                this.total_page += 1;
        }

        //alert(this.total_page+' pages,'+this.total_photo+' photos');
        
        var base_url = 'http://photo.renren.com/';
        var begin_page = this.page_from == -1? 0: this.page_from - 1;
        var end_page = this.page_to == -1? this.total_page - 1:
                        this.page_to > this.total_page? this.total_page - 1:
                            this.page_to - 1;
        for (var i = begin_page; i <= end_page; i+=1) {
            ret.push( base_url 
                + '/getalbum.do?id=' 
                + xnaja_app.utilities.get_url_param(url, 'id') 
                + '&owner=' 
                + xnaja_app.utilities.get_url_param(url, 'owner')
                + '&curpage=' + i);
        }
        //alert(ret.toSource());
        return ret;
    };


    this.emnu_photo_url = function (url) {
        var ret = [];
        var trs = xnaja_app.utilities.xnaja_go(url).match(/<span class="img">\s*<a href=".*?">/g);
        for (var each in trs) {
            ret.push(trs[each].match(/<a href="(.*)">/)[1]);
        }
        return ret;
    };
};
com.sxnsx.cexport(com.sxnsx.xnaja.downloader.renren, com.sxnsx.xnaja.downloader.abs_downloader);

